package tester.anticheeting;

import tester.*;

import javax.swing.*;
import java.util.List;
import java.util.Collections;

public class CheatingAnalyseThread extends Thread {
  private final PersonManager persons;
  private final Problem problem;
  private JTable tbProb;
  private List<Record> list;

  public CheatingAnalyseThread(PersonManager persons, Problem problem,
                               JTable tbProb, List<Record> list) {
    this.persons = persons;
    this.problem = problem;
    this.tbProb = tbProb;
    this.list = list;
  }

  public void run() {
    list.clear();
    tbProb.repaint();
    for (int i = 0; i < persons.personList.size(); i++) {
      Person p1 = persons.personList.get(i);
      if (p1.finished(problem.name)) {
        String fn1 = p1.getAbsoluteSourceFileName(problem);
        for (int j = i + 1; j < persons.personList.size(); j++) {
          Person p2 = persons.personList.get(j);
          if (p2.finished(problem.name)) {
            String fn2 = p2.getAbsoluteSourceFileName(problem);
            int sim = Matcher.similarity(
                FileUtils.fileToCompactString(fn1),
                FileUtils.fileToCompactString(fn2));
            if (sim >= Config.ANTI_CHEETING_THRESSHOLD) {
              list.add(new Record(p1, p2, problem, sim));
              tbProb.updateUI();
              tbProb.repaint();
            }
          }
        }
      }
    }
    Collections.sort(list);
    tbProb.repaint();
  }
}
